#!/usr/bin/env python

from pygraph.classes.digraph import digraph

def print_graph(g,n,d):
    if g.neighbors(n) == []:
        print d * "-", n
    else:
        print d * "-", n
        for h in g.neighbors(n):
            print_graph(g,h,d+1)

def make_dot(g):
    print "digraph d {"
    for e in g.edges():
        print "\t%i -> %i;" % (e[0],e[1])
    print "}"
#########################################
g = digraph()
g.add_nodes([1,2,3,4,5,6,7,8,9,10,11,12,13])
edges = ((8,9),(8,2),(1,2),(1,3),(9,10),(9,11),(11,12),(11,13),(3,4),(3,5),(4,6),(4,7))
for e in edges:
    g.add_edge(e[0],e[1])
	
"""for n in g.nodes():
    print "\tNode: %i Neighbors: %i Incidents: %i" % (n,len(g.neighbors(n)),len(g.incidents(n)))
    print "Neighbors",
    pprint(g.neighbors(n))
    print "Incidents",
    pprint(g.incidents(n))"""

top_nodes = []
for n in g.nodes():
    if len (g.incidents(n)) == 0:
        top_nodes.append(n)

for n in top_nodes:
    depth = 1
    print_graph(g,n,depth)

make_dot(g)
